package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 虎哥
 */
@RestController
@RequestMapping("spec")
public class SpecificationController {

    private final SpecGroupService groupService;
    private final SpecParamService paramService;

    public SpecificationController(SpecGroupService groupService, SpecParamService paramService) {
        this.groupService = groupService;
        this.paramService = paramService;
    }

    /**
     * 根据分类id查询规格组
     * @param id 分类id
     * @return 规格组的集合
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCategory(@RequestParam("id") Long id){
        List<SpecGroup> list = groupService.query().eq("category_id", id).list();
        return ResponseEntity.ok(SpecGroupDTO.convertEntityList(list));
    }

    /**
     * 根据调解查询规格参数
     * @param categoryId 分类id
     * @param groupId
     * @param searching
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) Boolean searching
    ){
        // 查询
        List<SpecParamDTO> list = paramService.querySpecParams(categoryId, groupId, searching);
        // 返回
        return ResponseEntity.ok(list);
    }

    /**
     * 根据分类id查询 规格组 及 组内的参数集合
     * @param id 分类id
     * @return 组集合
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id") Long id){
        List<SpecGroupDTO> list = groupService.querySpecList(id);
        return ResponseEntity.ok(list);
    }
}
